package com.glb.gilibuy.interceptor;

import com.glb.gilibuy.web.vo.AdminVO;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author: Yupeng Zhang
 * @Date: 2021-08-09 15:43
 */
public class AdminLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession = request.getSession();
        AdminVO admin = (AdminVO) httpSession.getAttribute("admin");
        System.out.println("拦截器拦截的session：" + httpSession);
        if (admin != null) {
            System.out.println("拦截器拦截的管理员：" + admin.toString() + "  " + admin.getAdminId());
        }
        if (admin != null) {
            return true;
        } else {
            response.sendRedirect("/adminLoginPage");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {

    }
}
